package com.powernode.business.controller;

import com.powernode.business.domain.Customer;
import com.powernode.business.service.CustomerService;
import com.powernode.common.core.controller.BaseController;
import com.powernode.common.core.domain.AjaxResult;
import com.powernode.common.core.domain.entity.SysDept;
import com.powernode.common.core.domain.model.LoginUser;
import com.powernode.common.core.page.TableDataInfo;
import com.powernode.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户管理的crud
 */
@RestController
@RequestMapping("business/customer")
@Api(tags = "客户管理接口")
public class CustomerController extends BaseController {
    //注入service层获取数据
    @Autowired
    private CustomerService customerService;

    //注入权限
    @Autowired
    private ISysDeptService iSysDeptService;

    @GetMapping("list")
    @ApiOperation("分页查询用户")
    //tableDataInfo获取表单参数
    public TableDataInfo list(Customer customer){
        //执行分页操作
        startPage();
        //拿到当前登录用户
        LoginUser loginUser = getLoginUser();
        Long deptId = loginUser.getDeptId();
        List<Customer> customerList = customerService.selectCustomerList(customer,deptId);
        return getDataTable(customerList);
    }

    /**
     * 查询所有部门  1
     * @return
     */
    @GetMapping("deptList")
    @ApiOperation("查询所有部门")
    public AjaxResult deptList(){
        //1.获取到当前用户
        LoginUser loginUser = getLoginUser();
        List<SysDept> sysDeptList = iSysDeptService.selectDeptListByDeptId(loginUser.getDeptId());
        System.out.println(sysDeptList);
        return AjaxResult.success(sysDeptList);
    }


    /**
     * 数据回显操作
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id回显客户信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id){
        //查询id对应的客户数据
        return AjaxResult.success(customerService.selectByPrimaryKey(id));
    }


    /**
     * 添加操作  1
     * @param customer
     * @return
     */
    @PostMapping
    @ApiOperation("添加用户")
    public AjaxResult add(@Validated @RequestBody Customer customer){
        //将登录账号的用户名修改到customer中的创建者
        LoginUser loginUser = getLoginUser();
        //customer.setCreateBy(getUsername());
        //所有的业务都在controller中写
        int i = customerService.addCustomer(customer,loginUser);
        return toAjax(customerService.insertSelective(customer));
    }

    /**
     * 修改操作
     * @param customer
     * @return
     */
    @PutMapping
    @ApiOperation("修改客户信息")
    public AjaxResult edit(@Validated @RequestBody Customer customer){
        //customer.setCreateBy(getUsername());
        LoginUser loginUser = getLoginUser();
        return toAjax(customerService.updateCustomer(customer,loginUser));
    }

    /**
     * 删除操作
     * @return
     */
    @DeleteMapping("{ids}")
    @ApiOperation("删除客户信息")
    public AjaxResult remove(@PathVariable("ids") List<Integer> ids){
        LoginUser loginUser = getLoginUser();
        return toAjax(customerService.deleteCustomer(ids,loginUser));
    }
}
